package com.sci.web.controller.admin.v2;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.sci.web.business.admin.SysEnumsBusiness;
import com.sci.web.controller.BaseController;
import com.sci.web.model.dto.PageDto;
import com.sci.web.model.genmodel.FileUploadfileinf;
import com.sci.web.model.genmodel.SysEnums;
import com.sci.web.model.genmodel.User;
import com.sci.web.service.admin.IndexService;

public class IndexController extends BaseController {
	
	public SysEnumsBusiness  sysEnumsBusiness= new SysEnumsBusiness();
	public IndexService  indexService= new IndexService();
	
	public void index(){
		render("login.jsp");
	}
	
	
	public void toIndex(){
		User user = super.getSessionByUser();
		if(user!=null){
			render("page_sys_enums.jsp");
			
		}else{
			redirect("/v2/admin");
		}
	}
	
	
	
	public void toMedia(){
		render("page_media.jsp");
	}
	
	
	
	public void toMediaGrid(){
		User user = super.getSessionByUser();
		int pageNumber = super.getBaseParToInt("p");
		String sysEnumsByKeyToValue = sysEnumsBusiness.getSysEnumsByKeyToValue("pageSize");
		int pageSize =sysEnumsByKeyToValue==null? 0: Integer.parseInt(sysEnumsByKeyToValue);
		String fileType = super.getBasePara("filetype");
		String searchStr = super.getBasePara("searchstr");
		Page<FileUploadfileinf> fileUploadfileinfPageInint = null;
		if(user!=null){
			pageNumber = pageNumber==0?1:pageNumber;
			Page<FileUploadfileinf> fileList = indexService.getFileUploadfileinfPageInint(pageNumber, pageSize, fileType, searchStr);
			if(fileList!=null){
				List<FileUploadfileinf> resultList = new ArrayList<FileUploadfileinf>();
				String webDomainName = sysEnumsBusiness.getSysEnumsByKeyToValue("webDomainName");
				String fileWebName = sysEnumsBusiness.getSysEnumsByKeyToValue("fileWebName");
				for (FileUploadfileinf entity : fileList.getList()) {
					System.out.println(entity.getThumbPath());
					entity.setThumbPath(webDomainName+File.separator+ fileWebName +File.separator+entity.getThumbPath());
					entity.setFilePath(webDomainName+File.separator+ fileWebName +File.separator+entity.getFilePath());
					resultList.add(entity);
				}
				//resultList ,
				fileUploadfileinfPageInint = new Page<FileUploadfileinf>(resultList , fileList.getPageNumber() , fileList.getPageSize() , fileList.getTotalPage() , fileList.getTotalRow());
			}
			setAttr("fileData", fileUploadfileinfPageInint);
			render("media_grid.jsp");
		}else{
			redirect("/v2/admin");
		}
	}
	
	
	
	
	/**  
	* @Title: getSysEnumPageInate  
	* @Description: 系统参数分页
	* @returnType void      
	* @author LiuFei
	* @date 下午10:10:51
	* @throws  
	*/
	public void getSysEnumPageInate(){
		PageDto page = new PageDto();
		Integer draw = getParaToInt("draw");
		Integer start = getParaToInt("start");
		Integer length = getParaToInt("length");
		String searchVal = getPara("search[value]");
		 //Page<BlogContent> blogPageInit = blogContentService.getBlogPageInit(user.getUserNo(), searchVal, (start/length)+1 , length);
		Integer pageSize = 15;
		Page<SysEnums> sysEnumsPageInate = sysEnumsBusiness.getSysEnumsPageInate(1, pageSize);
		page.setDraw(draw);
		page.setRecordsTotal(sysEnumsPageInate.getTotalRow());
		page.setRecordsFiltered(sysEnumsPageInate.getTotalRow());
		page.setData(sysEnumsPageInate.getList());
		renderJson(page);
	}
	
	
	/**  
	* @Title: getSysEnumsInfo  
	* @Description: 获取系统参数详情
	* @returnType void      
	* @author LiuFei
	* @date 下午10:11:08
	* @throws  
	*/
	public void getSysEnumsInfo(){
		String id = getPara("id");
		SysEnums sysEnumsById = sysEnumsBusiness.getSysEnumsById(id);
		if(sysEnumsById!=null){
			success(sysEnumsById);
		}else{
			error("error");
		}
	}
	
	

	
	
	
	

}
